package com.ldc.framework.adapter

import android.os.Build
import android.text.Html
import androidx.core.text.HtmlCompat
import androidx.recyclerview.widget.RecyclerView
import com.ldc.commonlibs.listener.OnRowListener
import com.ldc.commonres.common.BaseRvAdapter
import com.ldc.commonres.common.BaseRvViewHolder
import com.ldc.commonres.uts.TagUts
import com.ldc.framework.R
import com.ldc.framework.model.ArticleModel
import com.ldc.framework.model.ColletModel

class CollectArticleAdapter :
    BaseRvAdapter<ColletModel>(R.layout.framework_layout_view_item_collect) {
    private var onRowListener: OnRowListener<ColletModel>? = null
    private var onCollectListener: OnRowListener<ColletModel>? = null

    fun addOnRowListener(listener: OnRowListener<ColletModel>) {
        this.onRowListener = listener
    }

    fun addCollectRowListener(listener: OnRowListener<ColletModel>) {
        this.onCollectListener = listener
    }

    override fun convertTo(holder: BaseRvViewHolder, data: ColletModel, position: Int) {
        holder.setImage(R.id.iv_icon, data.envelopePic)
        holder.visibility(R.id.iv_icon, data.envelopePic.isNotBlank() || data.envelopePic != "")
        holder.setText(R.id.tv_title, data.title)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            val desc: CharSequence = Html.fromHtml(data.desc, HtmlCompat.FROM_HTML_MODE_COMPACT)
            holder.setText(
                R.id.tv_desc,
                desc
            )
        } else {
            holder.setText(R.id.tv_desc, data.desc)
        }
        holder.visibility(R.id.tv_desc, data.desc.isNotEmpty())
        holder.setText(R.id.tv_chapter_name, "${data.chapterName}")
        holder.setText(R.id.tv_author, data.author)
        holder.setText(R.id.tv_date, data.niceDate)
        holder.visibility(R.id.ll_zan, data.zan > 0)
        holder.setText(R.id.tv_zan, "${data.zan}")
        holder.setImage(R.id.iv_collect, com.ldc.commonres.R.drawable.res_icon_collect)
        holder.setText(
            R.id.tv_collect, "已收藏"
        )


        // 设置点击事件
        holder.addOnRowListener(holder.itemView, data, onRowListener)
        holder.addOnRowListener(R.id.ll_collect, data, onCollectListener)

    }
}